package com.example.tanhuanapp.controller;


import com.example.tanhuanapp.pojo.vo.ErrorResult;
import com.example.tanhuanapp.server.UserServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

/**
 * @author IT空门_门主
 * @date 2024/1/9
 * 登录
 */

@RestController
@RequestMapping("user")
@Api(tags = "用户登录")
public class UserController {
    @Autowired
    private UserServer userServer;

    @PostMapping(value = "loginVerification" , produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "登录(校验验证码)")
    public ResponseEntity<Object>  userLogin(@RequestBody Map<String,Object> param){
        //获取手机号和验证码,生成token
        String token =userServer.userLogin((String) param.get("phone"),param.get("verificationCode").toString());
        //传过来的数据是这样子的：isNew + "|" + token;
        //判断token是否为空
        if(StringUtils.isNotBlank(token)){
            Map<String, Object> result= new HashMap<>(2);
            String[] split = StringUtils.split(token, "|");
            boolean isNew = split[0].isEmpty();
            String tokenStr = split[1];
            result.put("isNew", isNew);
            result.put("token", tokenStr);
            return ResponseEntity.ok(result);
        }
        //登录错误,验证码错误
        ErrorResult.ErrorResultBuilder resultBuilder =
                ErrorResult.builder().errCode("000000").errMessage("验证码错误");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultBuilder.
                build());
    }
}
